package org.cytoscape.myapp.cytoscape_app_v1.internal.tasks.groups;

import java.awt.Component;
import java.util.Vector;

import org.cytoscape.application.CyApplicationManager;
import org.cytoscape.group.CyGroupFactory;
import org.cytoscape.group.CyGroupManager;
import org.cytoscape.myapp.cytoscape_app_v1.internal.UI.GroupsInfo;
import org.cytoscape.myapp.cytoscape_app_v1.internal.UI.SubComponent;
import org.cytoscape.myapp.cytoscape_app_v1.internal.UI.SubComponentInfo;
import org.cytoscape.myapp.cytoscape_app_v1.internal.grupos.Grupos;
import org.cytoscape.myapp.cytoscape_app_v1.internal.grupos.clasificadores.Clasificador;
import org.cytoscape.myapp.cytoscape_app_v1.internal.grupos.comparadores.Comparador;
import org.cytoscape.myapp.cytoscape_app_v1.internal.grupos.comparadores.ComparadorOr;
import org.cytoscape.myapp.cytoscape_app_v1.internal.grupos.criterios.Criterio;
import org.cytoscape.myapp.cytoscape_app_v1.internal.grupos.criterios.CriterioVariable;
import org.cytoscape.myapp.cytoscape_app_v1.internal.helpers.ModelUtils;
import org.cytoscape.myapp.cytoscape_app_v1.internal.tasks.GeneradorGrupos;
import org.cytoscape.work.Task;
import org.cytoscape.work.TaskIterator;
import org.cytoscape.work.TaskManager;
import org.cytoscape.work.TaskMonitor;

public class GroupsCreationComplexTask implements Task{

	private CyApplicationManager applicationManager;
	private CyGroupManager groupManager;
	private CyGroupFactory groupFactory;
	@SuppressWarnings("rawtypes")
	private Component[] subComponents;
	private GroupsInfo groupsInfo;

	public GroupsCreationComplexTask(CyApplicationManager applicationManager, CyGroupManager groupManager, CyGroupFactory groupFactory,
			  Component[] subComponents, GroupsInfo groupsInfo){
		
		this.applicationManager = applicationManager;
		this.groupManager = groupManager;
		this.groupFactory = groupFactory;
		this.subComponents = subComponents;
		this.groupsInfo = groupsInfo;
		
	}
	
	@Override
	public void run(TaskMonitor taskMonitor) throws Exception {
		
		
		taskMonitor.setTitle("Generando Grupos ");
		taskMonitor.setStatusMessage("Iniciando");
		taskMonitor.setStatusMessage("Procensando Nodos");
		Vector<Criterio> criterios = new Vector<Criterio>();
		
		for(Component sub:subComponents){
			SubComponentInfo info = new SubComponentInfo((SubComponent)sub);
			String[] words = info.getWords();
			Comparador primero = info.getComparador().FactoryMethod((String)info.getSensitive().CreateAttribute(words[0]).getValue());
			for(int i=1; i< words.length;i++){
				Comparador segundo= info.getComparador().FactoryMethod((String)info.getSensitive().CreateAttribute(words[i]).getValue());
				/*Agregar ComparadorCompuesto para OR, AND, etc hacerlo mas dinamico*/
				Comparador or = new ComparadorOr(primero,segundo);
				primero = or;
			}
			Criterio crit = new CriterioVariable(primero,((SubComponent) sub).getTextFieldNameGroup().getText() , info.getSensitive());
			criterios.add(crit);
		}
		
		Clasificador clasificador = groupsInfo.getClasificador().FactoryMethod(criterios);
		GeneradorGrupos generador = new GeneradorGrupos(applicationManager.getCurrentNetwork(), clasificador,
											new Grupos(groupManager, groupFactory, applicationManager.getCurrentNetwork()), 
													ModelUtils.getAllNodesWhitAttributes(applicationManager.getCurrentNetwork(), groupsInfo.getAttribute()));
		generador.generarGrupos();
		taskMonitor.setStatusMessage("Finalizando");
		
	}

	@Override
	public void cancel() {
		// TODO Auto-generated method stub
		
	}

}
